package uk.ac.lkl.lpp.model.xml;



import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import java.util.Map;
import java.util.logging.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import uk.ac.lkl.common.util.Property;
import uk.ac.lkl.lpp.model.PedagogyPlanModel;
import uk.ac.lkl.lpp.xml.XMLException;


public class ChunkPropertySetProcessor extends ModelElementProcessor implements ChunkPropertyNames{
	private static final Logger logger = Logger.getLogger("uk.ac.lkl.lpp");
	

    public ChunkPropertySetProcessor() {
        super("ChunkPropertySet");
    }


    public void convertDataToElements(PedagogyPlanModel plan,
                                      Document document,
                                      Element pedagogyPlanElement) {
        // should be done in base class
        Element parentElement = document.createElement("ChunkPropertySet");
        pedagogyPlanElement.appendChild(parentElement);
        
        // walk through base proerties and write them to ChunkProperty elements
        //
        // ID
        Element childElement = document.createElement(P_CHUNK);
        childElement.setAttribute(P_TYPE, P_TYPE_STRING);
        childElement.setAttribute(P_NAME, ID_PROPERTY);
        childElement.setAttribute(P_VALUE, plan.getId());
        parentElement.appendChild(childElement);
        
        // type
        childElement = document.createElement(P_CHUNK);
        childElement.setAttribute(P_TYPE, P_TYPE_STRING);
        childElement.setAttribute(P_NAME, TYPE_PROPERTY);
        childElement.setAttribute(P_VALUE, plan.getType());
        parentElement.appendChild(childElement);

        // name
        childElement = document.createElement(P_CHUNK);
        childElement.setAttribute(P_TYPE, P_TYPE_STRING);
        childElement.setAttribute(P_NAME, NAME_PROPERTY);
        childElement.setAttribute(P_VALUE, plan.getName());
        parentElement.appendChild(childElement);
        
        // description
        childElement = document.createElement(P_CHUNK);
        childElement.setAttribute(P_TYPE, P_TYPE_STRING);
        childElement.setAttribute(P_NAME, DESCRIPTION_PROPERTY);
        childElement.setAttribute(P_VALUE, plan.getDescription());
        parentElement.appendChild(childElement);

        // sub-type
        childElement = document.createElement(P_CHUNK);
        childElement.setAttribute(P_TYPE, P_TYPE_STRING);
        childElement.setAttribute(P_NAME, SUB_TYPE_PROPERTY);
        childElement.setAttribute(P_VALUE, plan.getSubType());
        parentElement.appendChild(childElement);

        // time units
        childElement = document.createElement(P_CHUNK);
        childElement.setAttribute(P_TYPE, P_TYPE_STRING);
        childElement.setAttribute(P_NAME, TIME_UNIT_PROPERTY);
        childElement.setAttribute(P_VALUE, plan.getTime_unit());
        parentElement.appendChild(childElement);

        // number of sub-elements
        childElement = document.createElement(P_CHUNK);
        childElement.setAttribute(P_TYPE, P_TYPE_POS);
        childElement.setAttribute(P_NAME, NUM_SUB_UNIT_PROPERTY);
        childElement.setAttribute(P_VALUE, String.valueOf(plan.getNSubs()));
        parentElement.appendChild(childElement);

        // credit time
        childElement = document.createElement(P_CHUNK);
        childElement.setAttribute(P_TYPE, P_TYPE_POS);
        childElement.setAttribute(P_NAME, CREDIT_TIME_PROPERTY);
        childElement.setAttribute(P_VALUE, String.valueOf(plan.getCreditTime()));
        parentElement.appendChild(childElement);
        
        // study time
        childElement = document.createElement(P_CHUNK);
        childElement.setAttribute(P_TYPE, P_TYPE_POS);
        childElement.setAttribute(P_NAME, STUDY_TIME_PROPERTY);
        childElement.setAttribute(P_VALUE, String.valueOf(plan.getStudyTime()));
        parentElement.appendChild(childElement);

        // contact time
        childElement = document.createElement(P_CHUNK);
        childElement.setAttribute(P_TYPE, P_TYPE_POS);
        childElement.setAttribute(P_NAME, CONTACT_TIME_PROPERTY);
        childElement.setAttribute(P_VALUE, String.valueOf(plan.getContactTime()));
        parentElement.appendChild(childElement);

        // number of students
        childElement = document.createElement(P_CHUNK);
        childElement.setAttribute(P_TYPE, P_TYPE_POS);
        childElement.setAttribute(P_NAME, NUM_STUD_PROPERTY);
        childElement.setAttribute(P_VALUE, String.valueOf(plan.getNumStudents()));
        parentElement.appendChild(childElement);

    }


    @Override
    public void convertElementToData(PedagogyPlanModel plan,
                                     Element parentElement) throws XMLException {
        try {
            NodeList nodes = parentElement
                    .getElementsByTagName(P_CHUNK);
            Element childElement = null;
            String type  = null;
            String name  = null;
            String value = null;  
            
            for (int i = 0; i < nodes.getLength(); i++) {
                childElement = (Element) nodes.item(i);
                type = childElement.getAttribute(P_TYPE);
                value = childElement.getAttribute(P_VALUE);
                name = childElement.getAttribute(P_NAME);
                // id
                if (name.equals(ID_PROPERTY)) {
                	plan.setId(value);
                	continue;
                }
                // type
                if (name.equals(TYPE_PROPERTY)) {
                	plan.setType(value);
                	continue;
                }
                // name
                if (name.equals(NAME_PROPERTY)) {
                	plan.setName(value);
                	continue;
                }
                // description
                if (name.equals(DESCRIPTION_PROPERTY)) {
                	plan.setDescription(value);
                	continue;
                }
                // sub-type
                if (name.equals(SUB_TYPE_PROPERTY)) {
                	plan.setSubType(value);
                	continue;
                }
                // time units
                if (name.equals(TIME_UNIT_PROPERTY)) {
                	plan.setTime_unit(value);
                	continue;
                }
                // number of sub-elements
                if (name.equals(NUM_SUB_UNIT_PROPERTY)) {
                	try {
                		plan.setNSubs(Integer.parseInt(value));
                	} catch (NumberFormatException e){
                		logger.severe("NumberFormatException reading nSubs: "+value);
                	}
                	continue;
                }
                // credit time
                if (name.equals(CREDIT_TIME_PROPERTY)) {
                	try {
                		plan.setCreditTime((Integer.parseInt(value)));
                	} catch (NumberFormatException e){
                		logger.severe("NumberFormatException reading nSubs: "+value);
                	}
                	continue;
                }
                // study time
                if (name.equals(STUDY_TIME_PROPERTY)) {
                	try {
                		plan.setStudyTime((Integer.parseInt(value)));
                	} catch (NumberFormatException e){
                		logger.severe("NumberFormatException reading nSubs: "+value);
                	}
                	continue;
                }
                // contact time
                if (name.equals(CONTACT_TIME_PROPERTY)) {
                	try {
                		plan.setContactTime((Integer.parseInt(value)));
                	} catch (NumberFormatException e){
                		logger.severe("NumberFormatException reading nSubs: "+value);
                	}
                	continue;
                }
                // number of students
                if (name.equals(NUM_STUD_PROPERTY)) {
                	try {
                		plan.setNumStudents((Integer.parseInt(value)));
                	} catch (NumberFormatException e){
                		logger.severe("NumberFormatException reading nSubs: "+value);
                	}
                	continue;
                }
            }
        }
        catch (IllegalArgumentException e) {
            throw new XMLException(e);
        }
        catch (ClassCastException e) {
            throw new XMLException(e);
        }
    }

}
